package com.yanggu.redisson.controller;

import org.redisson.api.RAtomicLong;
import org.redisson.api.RIdGenerator;
import org.redisson.api.RLongAdder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试分布式计算器简单使用
 */
@RestController
@RequestMapping("/test-counter")
public class TestCounterController {

    @Autowired
    @Qualifier("test-id-generator")
    private RIdGenerator idGenerator;

    @Autowired
    @Qualifier("test-long-adder")
    private RLongAdder longAdder;

    @Autowired
    @Qualifier("test-atomic-long")
    private RAtomicLong atomicLong;

    @GetMapping("test1")
    public void test1() {
        boolean result = idGenerator.tryInit(10L, 10L);
        System.out.println(result);
        System.out.println(idGenerator.nextId());
    }

    @GetMapping("test2")
    public void test2() {
        System.out.println(atomicLong.get());
        System.out.println(atomicLong.getAndIncrement());
        System.out.println(atomicLong.get());
        System.out.println(atomicLong.getAndAdd(10L));
        System.out.println(atomicLong.get());
        System.out.println(atomicLong.getAndDecrement());
        System.out.println(atomicLong.get());
        System.out.println(atomicLong.getAndSet(100L));
    }

    @GetMapping("test3")
    public void test3() {
        longAdder.increment();
        longAdder.add(1L);
        System.out.println(longAdder.sum());
    }

}
